package ui;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

import util.Java_JDBC;
import config.Config;
import config.PaintConfig;

public class ShowAnswerUI extends JPanel {
	private int width, hight;
	private JFrame frame;
	private JTable table;
	private Object[][] data;
	private String[] colname = {"题目编号","姓名","答案"};
	private JScrollPane jsPane;
	private Java_JDBC java_JDBC;
	private List<ArrayList<String>> result;

	public ShowAnswerUI() throws Exception{
		frame = new JFrame();
		java_JDBC = new Java_JDBC();
		width = PaintConfig.width;
		hight = PaintConfig.hight;
		// 设置布局
		this.setLayout(null);
		List<String> paperList = java_JDBC.searchPaperName();
		final JComboBox<Object> ca = new JComboBox<Object>(paperList.toArray());
		ca.setBounds(230, 0, 140, 30);
		ca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config.SelectedPaper = (String) ca.getSelectedItem();
//				result = java_JDBC.searchAnswer(Config.className,Config.SelectedPaper);
			}
		});
		ca.setSelectedIndex(0);
		
		JPanel jp = new JPanel();
		jp.setBounds(0, 50,width,hight);
		createTableData();  //创建表格所需数据 
        table = getSimpleTable();  
        table.setEnabled(false);
        jsPane = new JScrollPane(table);
        jp.add(jsPane);
        this.add(jp);
		this.add(ca);
		this.setLocation(width, hight);

		frame.getContentPane().add(this);
//		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, hight);
		frame.setLocationRelativeTo(null);
//		frame.setResizable(false);
		frame.setTitle("签到情况");
		frame.setVisible(true);
		
		ActionListener animate = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				repaint();
			}
        };
        Timer timer = new Timer(500,animate);
        timer.start();
	}

	@Override
	public void paintComponent(final Graphics g) {
		super.paintComponent(g);
		try {
			result = java_JDBC.searchAnswer(Config.className,Config.SelectedPaper);
			DefaultTableModel tableModel = (DefaultTableModel) (table.getModel());
			tableModel.getDataVector().clear();
			tableModel.fireTableDataChanged();
			if(result==null) {
				tableModel.setRowCount(1);
				return;
			}
			tableModel.setRowCount(result.size());
			for(int i=0;i<result.size();i++) {
				for(int j=0;j<result.get(i).size();j++) {
					table.setValueAt(result.get(i).get(j), i, j);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	 /**
     * 返回一个简单的表格
     * @return
     */
    private JTable getSimpleTable(){
    	DefaultTableModel model = new DefaultTableModel(data,colname);
        table = new JTable(model);
        return table;
    }
    
    /**
     * 给表格添加数据
     */
    private void createTableData(){
        data = new Object[1][3];
    }
	
//	public static void main(String[] args) throws Exception{
//		new ShowAnswerUI().setVisible(true);
//	}
}
